/**
 * 
 */
package hichecker.university;

import java.util.Comparator;

import javax.swing.SortOrder;

/**
 *
 */
public class StudentComparator implements Comparator<Student> {

	private CompareValue comp;
	private SortOrder order;
	
	public enum CompareValue {LASTNAME, FIRSTNAME, MATRIKEL, STUDYCOURSE}
	
	public StudentComparator(CompareValue comp, SortOrder order) {
		super();
		this.comp = comp;
		this.order = order;
	}
	
	@Override
	public int compare(Student o1, Student o2) {
		
		String fstValue = null;
		String sndValue = null;
		
		switch (comp) {
		case LASTNAME:
			fstValue = o1.getLastName();
			sndValue = o2.getLastName();
			break;
		case FIRSTNAME:
			fstValue = o1.getFirstName();
			sndValue = o2.getFirstName();
			break;
		case MATRIKEL:
			fstValue = o1.getMatrikel();
			sndValue = o2.getMatrikel();
			break;
		case STUDYCOURSE:
			fstValue = o1.getStudyCourse();
			sndValue = o2.getStudyCourse();
			break;
		}
		
		int compValue = order == SortOrder.ASCENDING ? 1 : -1;
		
		if (fstValue == null || fstValue.trim().length() == 0)
			return sndValue == null ? 0 : 1;
		if (sndValue == null || sndValue.trim().length() == 0)
			return fstValue == null ? 0 : -1;
		return fstValue.compareTo(sndValue) * compValue;
	}
	
}
